const {Users} = require('./db')
const {House} = require('./db')
const {Comment} = require('./db')
const {Sell} = require('./db')

//用户管理

//根据用户名查找用户
const findUser = (username) =>{
    return Users.findOne({username})
}
//根据id查找用户
const findOne = (id) =>{
    return Users.findOne({_id:id})
}
//注册用户
const signup = ({username,password,Tel,role})=>{
    const users = new Users({
        username,
        password,
        Tel,
        role,
    })
    return users.save()
    
}
//显示用户列表
const findList = ()=>{
    return Users.find().sort({_id:-1})
}
//删除用户
const remove = (id)=>{
    return Users.findByIdAndRemove(id)
}
//模糊查询用户
const findGrabbleName = (username) => {
    return Users.find({username:{$regex:`${username}`}})
}
//修改用户
const upDate = async({id,username,Tel,role})=>{
    return Users.updateOne({
        _id: id
    }, {
        $set: {
            username,
            Tel,
            role,
        }
    },function (err) {
        if (err) {
            console.log("更新出错", err);
        }
    });
   
}
exports.signup = signup
exports.findUser = findUser
exports.findList = findList
exports.remove = remove
exports.findGrabbleName = findGrabbleName
exports.upDate = upDate
exports.findOne = findOne


// 房屋管理

//房屋注册
const HouseSignup = ({position,resource,area,houseType,state,picture,price})=>{
    const house = new House({
        position,
        resource,
        area,
        houseType,
        state,
        picture,
        price
    })
    return house.save()
    
}

//房屋查找房子
const findHouse = (resource) =>{
    return House.findOne({resource})
}

//显示房屋列表
const HouseFindList = ()=>{
    return House.find().sort({_id:-1})
}

//删除房屋
const removeHouse = (id)=>{
    return House.findByIdAndRemove(id)
}

//模糊查询房屋
const findGrabbleHouse = (resource) => {
    return House.find({resource:{$regex:`${resource}`}})
}

//修改房屋信息
const upDateHouse = async({id,position,resource,area,houseType,state,picture,price})=>{
    return House.updateOne({
        _id: id
    }, {
        $set: {
            position,
            resource,
            area,
            houseType,
            state,
            picture,
            price
        }
    },function (err) {
        if (err) {
            console.log("更新出错", err);
        }
    });
   
}
//根据id查找用户
const findOneHouse = (id) =>{
    return House.findOne({_id:id})
}

exports.HouseSignup = HouseSignup
exports.findHouse = findHouse
exports.HouseFindList = HouseFindList
exports.removeHouse = removeHouse
exports.findGrabbleHouse = findGrabbleHouse
exports.upDateHouse = upDateHouse
exports.findOneHouse = findOneHouse

//评论管理

//添加评论
const signupComment = ({date,commentUsers,content,nice})=>{
    const comment = new Comment({
        date,//日期
        commentUsers,//评论用户
        content,//内容
        nice,//点赞
    })
    return comment.save()
    
}

//显示评论列表
const CommentFindList = ()=>{
    return Comment.find().sort({_id:-1})
}

//根据date查找评论
const findComment = (date) =>{
    return Comment.findOne({date})
}

//模糊查询评论
const findGrabbleComment = (commentUsers) => {
    return Comment.find({commentUsers:{$regex:`${commentUsers}`}})
}

//评论房屋
const removeComment = (id)=>{
    return Comment.findByIdAndRemove(id)
}

//根据id查找用户
const findOneComment = (id) =>{
    return Comment.findOne({_id:id})
}
exports.signupComment = signupComment
exports.CommentFindList = CommentFindList
exports.findComment = findComment
exports.findGrabbleComment = findGrabbleComment
exports.removeComment = removeComment
exports.findOneComment = findOneComment


//房屋注册
const SellSignup = ({position,resource,area,houseType,state,picture,price})=>{
    const sell = new Sell({
        position,
        resource,
        area,
        houseType,
        state,
        picture,
        price
    })
    return sell.save()
    
}

//房屋查找房子
const findSell = (resource) =>{
    return Sell.findOne({resource})
}

//显示房屋列表
const SellFindList = ()=>{
    return  Sell.find().sort({_id:-1})
}

//删除房屋
const removeSell = (id)=>{
    return  Sell.findByIdAndRemove(id)
}

//模糊查询房屋
const findGrabbleSell = (resource) => {
    return  Sell.find({resource:{$regex:`${resource}`}})
}

//修改房屋信息
const upDateSell = async({id,position,resource,area,houseType,state,picture,price})=>{
    return  Sell.updateOne({
        _id: id
    }, {
        $set: {
            position,
            resource,
            area,
            houseType,
            state,
            picture,
            price
        }
    },function (err) {
        if (err) {
            console.log("更新出错", err);
        }
    });
   
}
//根据id查找用户
const findOneSell = (id) =>{
    return  Sell.findOne({_id:id})
}

exports.SellSignup = SellSignup
exports.findSell = findSell
exports.SellFindList = SellFindList
exports.removeSell = removeSell
exports.findGrabbleSell = findGrabbleSell
exports.upDateSell = upDateSell
exports.findOneSell = findOneSell